/**
 * Copyright (c) 2007, Blue Hole Software. All rights reserved.
 * Code licensed under the Apache 2.0 License:
 * http://www.apache.org/licenses/
 */
package org.bhf.security.common;

import javax.security.auth.Subject;
import java.io.Serializable;
import java.util.Set;

/**
 * Public credential that provides a displayable full name. Each authenticated
 * <code>Subject</code> carries a <code>FullName</code> as a public
 * credential.
 */
public final class FullName implements Serializable
{
    private static final long               serialVersionUID    = 7930732926638008763L;
    private static final Class<FullName>    THIS_CLASS          = FullName.class;

    private final String fullName;

    /**
     * Create a <code>FullName</code> with the given name.
     *
     * @param fullName Full name. Must not be <code>null</code>.
     *
     * @throws java.lang.IllegalArgumentException if <code>first == null</code>
     *      or if <code>last == null</code>.
     */
    public FullName( final String fullName )
    {
        if( fullName == null )
            throw new IllegalArgumentException( "fullName == null" );

        this.fullName = fullName;
    }

    /**
     * Returns the full name.
     *
     * @return the full name. Will not return <code>null</code>.
     */
    public String           getName()
    {
        return fullName;
    }

    /**
     * Convenience method to return the <code>FullName</code> in a
     * <code>Subject</code>.
     *
     * @param subject The <code>Subject</code> from which to extract the
     *      <code>UserID</code>.  Must not be <code>null</code>.
     * @return The <code>FullName</code> of the given <code>Subject</code>
     *
     * @throws java.lang.IllegalArgumentException if <code>subject == null</code>
     *      or if <code>subject</code> does not contain exactly one
     *      <code>FullName</code> as a public credential.
     */
    public static FullName      getFullName( Subject subject )
    {
        if( subject == null )
            throw new IllegalArgumentException( "subject == null" );

        Set<FullName> set = subject.getPublicCredentials( THIS_CLASS );

        if( set.size() != 1 )
            throw new IllegalArgumentException( "subject.getPublicCredentials( FullName.class ).size() != 1" );

        return set.iterator().next();
    }

    /**
     * Convenience method to return the <code>FullName</code> of the
     * <code>FullName</code> associated with the current thread.
     * @return The <code>Location</code> of the given <code>Subject</code>
     *
     * @throws java.lang.IllegalArgumentException if <code>subject</code> does not
     *      contain exactly one <code>FullName</code> as a public credential
     *      or if there is no code>Subject</code> associated with the
     *      current thread.
     */
    public static FullName      getFullName()
    {
        return getFullName( ContextSubject.getContextSubject() );
    }

    /**
     * Returns the hash code based on first and last name.
     *
     * @return the hash code.
     */
    public int              hashCode()
    {
        return fullName.hashCode();
    }

    /**
     * Equality based on first and last name.
     *
     * @param o Object against which to compare
     *
     * @return <code>true</code> iff <code>o</code> is a <code>FullName</code>
     *      with the same first and last names.
     */
    public boolean          equals( Object o )
    {
        try
        {
            FullName that = (FullName)o;
            return that.fullName.equals( fullName );
        }
        catch( Exception e )
        {
            return false;
        }
    }
}


